package net.neiquan.applibrary.utils;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

/**
 * 作者 ： 程秀红
 * 日期 ： 2016/3/14.
 */
public class PopuWindowsUtils {
    private Activity mContext;
    private PopupWindow mTwoPopu;
    private int mLayoutid, mWidth, mHight;
    private View mContView, mView;

    public PopuWindowsUtils(Activity context, int layoutid, int width, int hight, View view) {
        mContext = context;
        mContView = View.inflate(mContext, layoutid, null);
        mLayoutid = layoutid;
        mWidth = width;
        mHight = hight;
        mView = view;
    }

    public PopupWindow getPopupWindow() {
        return mTwoPopu;
    }

    public View showDown() {
        mTwoPopu = new PopupWindow(mContView, mWidth, mHight, true);
        mTwoPopu.setBackgroundDrawable(new BitmapDrawable());
        // 设置PopupWindow的弹出和消失效果
        // mTwoPopu.setAnimationStyle(R.style.AlertChooserAnimation);
        mTwoPopu.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        mTwoPopu.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        backgroundAlpha(0.6f);
        mTwoPopu.setOnDismissListener(new poponDismissListener());
        // 点击其他地方消失
        mContView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
//                mTwoPopu.dismiss();
                return true;
            }
        });
        mTwoPopu.showAsDropDown(mView);
        return mContView;
    }
    public View showWindBottom() {
        mTwoPopu = new PopupWindow(mContView, mWidth, mHight, true);
        mTwoPopu.setBackgroundDrawable(new BitmapDrawable());
        // 设置PopupWindow的弹出和消失效果
        // mTwoPopu.setAnimationStyle(R.style.AlertChooserAnimation);
        mTwoPopu.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        mTwoPopu.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        backgroundAlpha(0.6f);
        mTwoPopu.setOnDismissListener(new poponDismissListener());
        // 点击其他地方消失
        mContView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
//                mTwoPopu.dismiss();
                return true;
            }
        });
        mTwoPopu.showAtLocation(mView,Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
        return mContView;
    }
    public View showCenter() {
        mTwoPopu = new PopupWindow(mContView, mWidth, mHight, true);
        mTwoPopu.setBackgroundDrawable(new BitmapDrawable());
        // 设置PopupWindow的弹出和消失效果
        // mTwoPopu.setAnimationStyle(R.style.AlertChooserAnimation);
        mTwoPopu.setSoftInputMode(PopupWindow.INPUT_METHOD_NEEDED);
        mTwoPopu.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        backgroundAlpha(0.6f);
        mTwoPopu.setOnDismissListener(new poponDismissListener());
        // 点击其他地方消失
        mContView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
//                mTwoPopu.dismiss();
                return true;
            }
        });
        mTwoPopu.showAtLocation(mView, Gravity.CENTER, 0, 0);
        return mContView;
    }
    public void setPianyiDown(int x,int y) {
        mTwoPopu.dismiss();
        mTwoPopu.showAsDropDown(mView, x, y);
    }
    public PopupWindow getmTwoPopu() {
        return mTwoPopu;
    }

    public void dismissPopuWindow(){
        if(mTwoPopu.isShowing()){
            mTwoPopu.dismiss();
        }

    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        mContext.getWindow().setAttributes(lp);
    }

    /**
     * 添加新笔记时弹出的popWin关闭的事件，主要是为了将背景透明度改回来
     *
     * @author cg
     */
    class poponDismissListener implements PopupWindow.OnDismissListener {

        @Override
        public void onDismiss() {
            // TODO Auto-generated method stub
            //Log.v("List_noteTypeActivity:", "我是关闭事件");
            backgroundAlpha(1f);
        }

    }
}
